package org.jeecg.modules.bid.service.impl;

import org.jeecg.modules.bid.entity.BidProclamation;
import org.jeecg.modules.bid.entity.BidProclamationAttachment;
import org.jeecg.modules.bid.mapper.BidProclamationAttachmentMapper;
import org.jeecg.modules.bid.mapper.BidProclamationMapper;
import org.jeecg.modules.bid.service.IBidProclamationService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 采购信息公告
 * @Author: jeecg-boot
 * @Date:   2025-04-26
 * @Version: V1.0
 */
@Service
public class BidProclamationServiceImpl extends ServiceImpl<BidProclamationMapper, BidProclamation> implements IBidProclamationService {

	@Autowired
	private BidProclamationMapper bidProclamationMapper;
	@Autowired
	private BidProclamationAttachmentMapper bidProclamationAttachmentMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BidProclamation bidProclamation, List<BidProclamationAttachment> bidProclamationAttachmentList) {
		bidProclamationMapper.insert(bidProclamation);
		if(bidProclamationAttachmentList!=null && bidProclamationAttachmentList.size()>0) {
			for(BidProclamationAttachment entity:bidProclamationAttachmentList) {
				//外键设置
				entity.setProclamationId(bidProclamation.getId());
				bidProclamationAttachmentMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BidProclamation bidProclamation,List<BidProclamationAttachment> bidProclamationAttachmentList) {
		bidProclamationMapper.updateById(bidProclamation);
		
		//1.先删除子表数据
		bidProclamationAttachmentMapper.deleteByMainId(bidProclamation.getId());
		
		//2.子表数据重新插入
		if(bidProclamationAttachmentList!=null && bidProclamationAttachmentList.size()>0) {
			for(BidProclamationAttachment entity:bidProclamationAttachmentList) {
				//外键设置
				entity.setProclamationId(bidProclamation.getId());
				bidProclamationAttachmentMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		bidProclamationAttachmentMapper.deleteByMainId(id);
		bidProclamationMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			bidProclamationAttachmentMapper.deleteByMainId(id.toString());
			bidProclamationMapper.deleteById(id);
		}
	}
	
}
